import MainHome from "../facility/priveteFacility/MainHome";
import { FacilityType } from "../info/GameEnum";
import { Global } from "../info/Global";
import PlayerManager from "../manager/PlayerManager";
import ControlledPlayer from "../player/ControlledPlayer";
import Player from "../player/Player";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NoviceGuide extends cc.Component {

    @property(cc.Label)
    infor: cc.Label = null;

    @property(cc.Label)
    diamondLabel: cc.Label = null;

    @property(cc.Label)
    oilLabel: cc.Label = null;

    player: ControlledPlayer;

    /**
     * 当前展示的任务进度
     */
    showProgress: number = 0;

    /**
     * 每个任务的进度
     */
    taskProgress: number[] = [];

    public static EventType = {
        主基地新增矿工: "主基地新增矿工",
        主基地升级: "主基地升级",
        建造设施: "建造设施",
        派遣侦察机: "派遣侦察机",
        侦察到敌人的建筑: "侦察到敌人的建筑",
        矿工速度升级: "矿工速度升级",
        给敌人建筑发射一枚炮弹: "给敌人建筑发射一枚炮弹"
    }

    start() {
        if (Global.gameInfo.guideOver) {
            this.node.destroy();
        }
        this.taskProgress = Global.noviceGuide.map(i => 0);
        this.player = PlayerManager.instance.controllerPlayer;
        this.player.node.on(NoviceGuide.EventType.主基地新增矿工, this.onAddMiner, this);
        this.player.node.on(NoviceGuide.EventType.主基地升级, this.onMainHomeLevelUp, this);
        this.player.node.on(NoviceGuide.EventType.建造设施, this.onAddFacility, this);
        this.player.node.on(NoviceGuide.EventType.派遣侦察机, this.onDispatchScout, this);
        this.player.node.on(NoviceGuide.EventType.侦察到敌人的建筑, this.onFindEnemy, this);
        this.player.node.on(Player.EventType.更新矿工速度等级, this.onMinerSpeedUp, this);
        this.player.node.on(NoviceGuide.EventType.给敌人建筑发射一枚炮弹, this.onFire, this);
        this.referhInfo();
    }

    /**
     * 给敌人建筑发射一枚炮弹   
     */
    onFire() {
        this.taskProgress[11] = -1;
        this.player.node.off(NoviceGuide.EventType.给敌人建筑发射一枚炮弹, this.onFire, this);
        this.checkAward();
    }

    /**
     * 矿工增加
     */
    onAddMiner() {
        let minerCount = (<MainHome>this.player.facilityArr[FacilityType.主基地][0]).minerArr.length;
        if (this.taskProgress[0] >= 0) {
            this.taskProgress[0] = minerCount
            if (this.taskProgress[0] >= 2) {
                this.taskProgress[0] = -1;//完成
            }
        }
        if (this.taskProgress[1] >= 0) {
            this.taskProgress[1] = minerCount
            if (this.taskProgress[1] >= 6) {
                this.taskProgress[1] = -1;//完成
                this.player.node.off(NoviceGuide.EventType.主基地新增矿工, this.onAddMiner, this);
            }
        }
        this.checkAward();
    }

    /**
     * 主基地升级
     */
    onMainHomeLevelUp() {
        let mainhomeLevel = this.player.mainHomeLevel;
        if (this.taskProgress[2] >= 0) {
            this.taskProgress[2] = mainhomeLevel >= 1 ? -1 : mainhomeLevel;
        }
        if (this.taskProgress[7] >= 0) {
            if (mainhomeLevel >= 2) {
                this.taskProgress[7] = -1;
                this.player.node.off(NoviceGuide.EventType.主基地升级, this.onMainHomeLevelUp, this);
            } else {
                this.taskProgress[7] = mainhomeLevel;
            }
        }
        this.checkAward();
    }

    /**
     * 建造设施
     */
    onAddFacility(facilityType: FacilityType) {
        switch (facilityType) {
            case FacilityType.矿机:
                this.taskProgress[3] = -1;
                break;
            case FacilityType.侦察机基地:
                this.taskProgress[4] = -1
                break;
            case FacilityType.近防炮:
                this.taskProgress[6] = -1
                break;
            case FacilityType.采油机:
                this.taskProgress[10] = -1
                break;
            default:
                return;
        }
        if (this.taskProgress[3] == -1 && this.taskProgress[4] == -1 && this.taskProgress[6] == -1 && this.taskProgress[10] == -1) {
            this.player.node.off(NoviceGuide.EventType.建造设施, this.onAddFacility, this);
        }
        this.checkAward();
    }

    /**
     * 矿工速度升级
     */
    onMinerSpeedUp() {
        this.taskProgress[8] = this.player.minerSpeedLevel >= 1 ? -1 : this.player.minerSpeedLevel;
        this.player.node.off(NoviceGuide.EventType.矿工速度升级, this.onMinerSpeedUp, this);
        this.checkAward();
    }

    /**
     * 发射侦察机
     */
    onDispatchScout() {
        this.taskProgress[5] = -1;
        this.player.node.off(NoviceGuide.EventType.派遣侦察机, this.onDispatchScout, this);
        this.checkAward();
    }

    /**
     * 当侦察机发现敌人建筑
     */
    onFindEnemy() {
        this.taskProgress[9] = -1;
        this.player.node.off(NoviceGuide.EventType.侦察到敌人的建筑, this.onFindEnemy, this);
        this.checkAward();
    }

    /**
     * 检测引导是否完成
     */
    checkAward() {
        this.unscheduleAllCallbacks();
        
        if (this.taskProgress[this.showProgress] == -1) {
            let award = Global.noviceGuide[this.showProgress].奖励
            this.player.addDiamondAndOil(award.diamond, award.oil);
            this.showProgress++;
            if(this.showProgress < this.taskProgress.length){
                this.referhInfo();
                this.schedule(() => {
                    this.checkAward()
                }, 1.5);
            }else{
                this.node.destroy();
                Global.gameInfo.guideOver = 1;
            }
        }
    }

    /**
     * 刷新信息
     */
    referhInfo() {
        let cur = Global.noviceGuide[this.showProgress];
        this.infor.string = cur.说明;
        this.diamondLabel.node.parent.active = cur.奖励.diamond > 0;
        this.oilLabel.node.parent.active = cur.奖励.oil > 0;
        this.diamondLabel.string = "+" + cur.奖励.diamond
        this.oilLabel.string = "+" + cur.奖励.oil
    }
}
